import { defineStore } from 'pinia'

import {} from './api'

import { generateRoutes, configRoutes, redirectRoutes } from '@/router'

import { RouterState } from './data.d'
import type { RouteRecordRaw } from 'vue-router'

export const useRouterStore = defineStore('routerStore', {
  state: (): RouterState => ({
    originalRoutes: [], // 原始路由
    menuRoutes: [], // 菜单路由
    routes: [],
  }),
  actions: {
    async getMenuRoutes() {
      const filterConfigRoutes = generateRoutes(configRoutes)
      const filterRedirectRoutes = generateRoutes(redirectRoutes)

      this.originalRoutes = [...filterConfigRoutes, ...filterRedirectRoutes]
      this.menuRoutes = filterConfigRoutes.filter((route) => !route.meta!.hidden)

      return this.originalRoutes
    },
    setRoutes(routers: RouteRecordRaw[]) {
      this.routes = routers
    },
    setMenuRoute(route: RouteRecordRaw) {
      this.menuRoutes = route.children!
    },
  },
})
